package ru.alt22.visor.php.ui.wizards;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import ru.alt22.visor.php.core.codegen.PHPFile;
import ru.alt22.visor.php.core.codegen.PHPInterface;
import ru.alt22.visor.php.core.codegen.PHPTypeBuilder;
import ru.alt22.visor.php.ui.core.wizards.NewTypeWizard;
import ru.alt22.visor.php.ui.core.wizards.TypePageOptions;

public class NewInterfaceWizard extends NewTypeWizard {
	private NewInterfaceWizardPage page;

	public void addPages() {
		page = new NewInterfaceWizardPage(selection);
		addPage(page);
	}

	public boolean performFinish() {
		final TypePageOptions options = new TypePageOptions(page);
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {
					doFinish(options, monitor);
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
			return false;
		}
		return true;
	}

	private void doFinish(TypePageOptions options, IProgressMonitor monitor) throws CoreException {
		monitor.beginTask("Creating " + options.getTypeName() + "...", 2);
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource resource = root.findMember(new Path(options.getFolderName()));
		IProject project = resource.getProject();
		if (!resource.exists() || !(resource instanceof IContainer)) {
			throwCoreException("Container \"" + options.getFolderName() + "\" does not exist.");
		}
		IContainer container = (IContainer) resource;
		final IFile file = container.getFile(new Path(options.getTypeName() + PHPTypeBuilder.CLASS_EXTENSION));
		try {
			InputStream stream = openContentStream(options, project);
			if (file.exists()) {
				file.setContents(stream, true, true, monitor);
			} else {
				file.create(stream, true, monitor);
			}
			stream.close();
		} catch (IOException e) {
		}
		monitor.worked(1);
		monitor.setTaskName("Opening file for editing...");
		getShell().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchPage page =
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				try {
					IDE.openEditor(page, file, true);
				} catch (PartInitException e) {
				}
			}
		});
		monitor.worked(1);
	}

	private InputStream openContentStream(TypePageOptions options, IProject project) {
		PHPFile file = new PHPFile();
		PHPInterface phpInterface = new PHPInterface(options.getTypePrefix() + options.getTypeName());
		file.addEntry(phpInterface);

		for (int i = 0; i < options.getImplements().length; i++) {
			phpInterface.addSuperclass(options.getImplements()[i].getName());
		}

		return new ByteArrayInputStream(file.toString().getBytes());
	}

}